package com.sf.hackathons.app.base;

import android.app.ActivityManager;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;


import com.sf.hackathons.app.utils.ApplicationUtils;
import com.sf.hackathons.lib.store.AppContext;

import java.util.List;

/**
 * Created by guofanghua on 2017/9/20.
 */
public class BaseApplication extends MultiDexApplication {
    private static BaseApplication sInstance;



    @Override
    public void onCreate() {
        super.onCreate();
        if (isMainProcess(getApplicationContext())) {
            init();
        }
    }

    private void init() {
        if (isMainProcess(getApplicationContext())) {

            initSdk();
        }
    }



    private void initSdk() {
        // TODO: 2017/9/20  初始化sdk
        AppContext.setAppContext(this);
        ApplicationUtils.initHttp(this);//初始化http
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);

    }
    /**
     * 获取进程名
     */
    public static String getProcessName(Context cxt, int pid) {
        ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
            if (procInfo.pid == pid) {
                return procInfo.processName;
            }
        }
        return null;
    }
    public static boolean isMainProcess(Context context) {
        if (context == null) {
            return false;
        }
        boolean defaultProcess = false;
        String processName = getProcessName(context, android.os.Process.myPid());
        if (processName != null) {
            defaultProcess = processName.equals(context.getPackageName());

        }
        return defaultProcess;
    }
}
